<?php
class NotificationsType{
    public $like=1;
    public $share=2;
    public $comment=3;
    public $status_update=4;
    public $post=5;
    public $friend_request=6;
    public $friend_accept=7;
    public $inbox=8;
    public $birthday=9;
    public $techno=10;
    public $event=11;
    public $page=12;
    public $games_app=13;
    public $entertainment=14;
    public $blog=15;
}

 class PrivacyTypes{
    public $public=1;
    public $friends=2;
    public $specific_friends=3;
    public $cluster=4;
    public $friends_of_friend=5;
    public $only_me=6;
 }

class UserPrivacySettings{
    public $contact_me_for_friendship=0;
    public $search_engine_like_on_my_dna=0;
    public $email_address=0;
    public $contact_number=0;
    public $address=0;
    public $dob=0;
    public $fax_number=0;
    public $family=0;
    public $blog=0;
    public $voice_comment=0;
    public $download_photos=0;
    public $newsfeeds=0;
    
    function __construct(){
        $ptypes=new PrivacyTypes();
        $this->contact_me_for_friendship=$ptypes->public;
        $this->search_engine_like_on_my_dna=$ptypes->public;
        $this->email_address=$ptypes->public;
        $this->contact_number=$ptypes->public;
        $this->address=$ptypes->public;
        $this->dob=$ptypes->public;
        $this->fax_number=$ptypes->public;
        $this->family=$ptypes->public;
        $this->blog=$ptypes->public;
        $this->voice_comment=$ptypes->public;
        $this->download_photos=$ptypes->public;
        
        $this->newsfeeds=$ptypes->public;
    }
}

class DetailedJsonTemplates{
    public $maxParticipantsInclude=3;
    
    public function getFixedLengthStr($str,$lengthToTrimAfter=25){
        
    }
    
    public function getParticipantName($uid,$linkToProfile=false){
        //html output   
    }
    
    public function getLikesJson($detailedArr){
        $msgStr="{participants name} has liked on a post \"{title}\" posted by {owner name}";
        $participantsarr=$detailedArr->participantsIds;
        $posttitle=getFixedLengthStr($detailedArr->titlemsg,25);
        $postOwner=getParticipantName($detailedArr->newsOwnerId);
        
        $participantsName="";
        if(count($participantsarr)>0){
            $participantsName="";
            
            if(count($participantsarr) >= $this->maxParticipantsInclude){
                for($pCounts=0;$pCounts<($this->maxParticipantsInclude-1);$pCounts++){
                    $participantsName.=getParticipantName($participantsarr[$pCounts]).",";
                }
                $participantsName=substr($participantsName,0,strlen($participantsName)-1);
                $participantsName.=" and ".(count($participantsarr)-$pCounts)." others ";
                //vikas, jitesh and 2 others
            }
            else{
                for($pCounts=0;$pCounts<count($participantsarr);$pCounts++){
                    $participantsName.=getParticipantName($participantsarr[$pCounts])." and ";
                }
                $participantsName=substr($participantsName,0,strlen($participantsName)-4);
                //vikas and Jr
            }
            if(count($participantsarr)>1){
                $templateMsg=$msgStr;
                $templateMsg=str_replace("{participants name}",$participantsName,$msgStr);
                $templateMsg=str_replace("{title}",$posttitle,$msgStr);
                $templateMsg=str_replace("{owner name}",$postOwner,$msgStr);                                        
            }
                    
        }        
        $template='{"record_id":'.$detailedArr->recordId.',"msg":'.$templateMsg.',"module":'.$detailedArr->moduleName.'}';
    }
}

class DetailJson{
    public $recordId;
    public $message;
    public $actionByUserId;
    public $actionByUserName;
    public $actionByUserProfilePicpath;
    public $actionByUserLink;
    public $actionByUserIsCelibrity;
    public $actionByCity;
    public $actionByCountry;
    public $info;
    public $notificationID;
    public $daysLeft;
    function __construct() {
       try{
            $this->recordId=0;
            $this->message="";
            $this->actionByUserId=0;
            $this->actionByUserIsCelibrity=0;
            $this->actionByUserLink="";
            $this->actionByUserName="";
            $this->actionByUserProfilePicpath="";
            $this->actionByCity="";
            $this->actionByCountry="";
            $this->info="";
            $this->notificationID=0;
            $this->daysLeft="";
        }
        catch(Exception $ex){
            die('DetailJson:__construct():: \t'. $ex->getMessage(). "\n");   
        }
   }
}

class PostNotifyJson{
    public $postid;
    public $lcount;
    public $scount;
    public $ccount;
    public $LikeDetails;
    public $CommentDetails;
    public $ShareDetails;
    
    function __construct() {
       try{
            $LikeDetails= new DetailJson();
            $CommentDetails= new DetailJson();
            $ShareDetails= new DetailJson();
        }
        catch(Exception $ex){
            die('ReadNotifications.php:__construct():: \t'. $ex->getMessage(). "\n");   
        }
   }
    
}
 class Notifications{
    
    //need to check if detailed notification is needed or count notifications
    public $isDetailedNotification=false;
    
    //detailed notification values
    public $recordId=0;
    public $participantsIds=array();
    public $totalParticipants=0;
    public $newsFromOwnerUsername='';
    public $newsOwnerId=0;
    public $titlemsg="";
    public $moduleName="";//link to .php page
    
    
    private $loggedInUserId=0;
    public $pdodbobj;
    public $connstatus=false;
    public $lastInsertedID=0;
    
    function __construct() {
       try{
            $this->pdodbobj= new DB();
            $this->connstatus=$this->pdodbobj->connect();
            
            if(isset($_SESSION['Allyfolio_User_ID'])&& !empty($_SESSION['Allyfolio_User_ID']) && is_numeric($_SESSION['Allyfolio_User_ID'])){
                $this->loggedInUserId=trim($_SESSION['Allyfolio_User_ID']);    
            }
            else{
                die('ReadNotifications.php:__construct():: Invalid Session => '.$_SESSION['Allyfolio_User_ID'].' \t'. "\n");
            }
        }
        catch(Exception $ex){
            die('ReadNotifications.php:__construct():: \t'. $ex->getMessage(). "\n");   
        }
   }
  
    public function getUserDetails($uid){
        $urecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getUserDetails($uid)"));
        if(is_array($urecords) && count($urecords)>0){
            $urecords=$urecords[0];
        }
        return $urecords;
    }
  //str_format('INSERT INTO `posts` (title,text,tags,status_id) VALUES("{1}","{2}","{3}",{4})', 'title', 'text', 'tags', 1);
    public function str_format()
    {
        $args = func_get_args();
        $str = mb_ereg_replace('{([0-9]+)}', '%\\1$s', array_shift($args));
        return vsprintf($str, array_values($args));
    }
    
    public function getUserDetailsJSON($objDetailJson,$isCountRequest=false){       
        $detailedJsonStr="[";
        if(!$isCountRequest){
            foreach($objDetailJson as $detailedJson){
                $singleJsonStr="{
                    \"uid\":{1},
                    \"uname\":\"{2}\",
                    \"upicpath\":\"{3}\",
                    \"ulink\":\"{4}\",
                    \"city\":\"{5}\",
                    \"country\":\"{6}\",
                    \"info\":{7},
                    \"notification_id\":{8},
                    \"days_left\":\"{9}\"
                }";
                
                 //echo "<pre>";
        //echo($detailedJson->info);
        //echo (strlen(trim($detailedJson->info))==0))
        //die("getUserDetailsJSON");
        
                $detailedJsonStr.=$this->str_format(
                                            $singleJsonStr,
                                            $detailedJson->actionByUserId,
                                            $detailedJson->actionByUserName,
                                            (strlen(trim($detailedJson->actionByUserProfilePicpath))==0)?"images/default_user_pic.jpg":$detailedJson->actionByUserProfilePicpath,
                                            $detailedJson->actionByUserLink,
                                            (strlen(trim($detailedJson->actionByCity))==0)?"":$detailedJson->actionByCity,
                                            (strlen(trim($detailedJson->actionByCountry))==0)?"":$detailedJson->actionByCountry,                                            
                                            (strlen(trim($detailedJson->info))==0)?"[]":$detailedJson->info,
                                            $detailedJson->notificationID,
                                            $detailedJson->daysLeft                                           
                                            ).",";                  
            }                  
            if(strstr($detailedJsonStr,",")){
                $detailedJsonStr=substr($detailedJsonStr,0,strlen($detailedJsonStr)-1);
            }
            
        }
        $detailedJsonStr.="]";
        return $detailedJsonStr;
    }
    public function getDetailedJson($objDetailJson){
        $detailedJsonStr="[";        
        foreach($objDetailJson as $detailedJson){
            $singleJsonStr="{
                \"uid\":{1},
                \"uname\":'{2}',
                \"upicpath\":'{3}',
                \"ulink\":'{4}',
                \"message\":'{5}'
            }";
            $detailedJsonStr.=$this->str_format(
                                        $singleJsonStr,
                                        $detailedJson->actionByUserId,
                                        $detailedJson->actionByUserName,
                                        $detailedJson->actionByUserProfilePicpath,
                                        $detailedJson->actionByUserLink,
                                        $detailedJson->message).",";                  
        }
        if(count($objDetailJson)>1){
            $detailedJsonStr=substr($detailedJsonStr,0,strlen($detailedJsonStr)-1);
        }        
        $detailedJsonStr.="]";
    }
    public function getLikeShareCommentJson($objPostNotifyJsonArr,$isCountRequest=true){
        //print_r($objPostNotifyJsonArr);
        //die("getLikeShareCommentJson");
        $detailedJson="[";
        foreach($objPostNotifyJsonArr as $objPostNotifyJson){
            $postId=$objPostNotifyJson->postid;
            $totalLikes=$objPostNotifyJson->lcount;
            $totalComments=$objPostNotifyJson->ccount;
            $totalShares=$objPostNotifyJson->scount;
            
            if(!$isCountRequest){
                $detailedLikes=$this->getDetailedJson($objPostNotifyJson->LikeDetails);
                $detailedComments=$this->getDetailedJson($objPostNotifyJson->CommentDetails);
                $detailedShares=$this->getDetailedJson($objPostNotifyJson->ShareDetails);
            }
            
            $jsonStr="{
                        \"postid\":{1},
                        \"likes_count\":{2},
                        \"comments_count\":{3},
                        \"shares_count\":{4}";
                        if(!$isCountRequest){
                            $jsonStr.="\"Likes\":{5},
                            \"Comments\":{6},
                            \"Shares\":{7}";
                        }
            $jsonStr.="}";
            if($isCountRequest){
                $detailedJson.=$this->str_format($jsonStr, is_numeric($postId)?$postId:0, is_numeric($totalLikes)?$totalLikes:0, is_numeric($totalComments)?$totalComments:0, is_numeric($totalShares)?$totalShares:0).",";
            }
            else{
                $detailedJson.=$this->str_format($jsonStr, is_numeric($postId)?$postId:0, is_numeric($totalLikes)?$totalLikes:0, is_numeric($totalComments)?$totalComments:0, is_numeric($totalShares)?$totalShares:0,$detailedLikes,$detailedComments,$detailedShares).",";
            }   
        }
        if(strstr($detailedJson,",")){
            $detailedJson=substr($detailedJson,0,strlen($detailedJson)-1);
        }
        $detailedJson.="]";
        return $detailedJson; 
    }
    
    public function getFeedLikeCommentShareCount($feedid,$typeStr="like",$commentId=0){
        $fcount=0;
        switch($typeStr){
            case "like":
                $this->pdodbobj->query("CALL getTotalLikeCountsForFeeds($feedid,@ucount)");
                break;
            case "commentlike":
                //die("CALL getTotalLikeCountsForComments($feedid,$commentId,@ucount)");
                $this->pdodbobj->query("CALL getTotalLikeCountsForComments($feedid,$commentId,@ucount)");
                break;
            case "share":
                $this->pdodbobj->query("CALL getTotalShareCountsForFeeds($feedid,@ucount)");
                break;
            case "comment":
                $this->pdodbobj->query("CALL getTotalCommentCountsForFeeds($feedid,@ucount)");
                break;
            default:
                $this->pdodbobj->query("CALL getTotalLikeCountsForFeeds($feedid,@ucount)");
                break;
        }
        
        $feedcountarr= $this->pdodbobj->fetch_array($this->pdodbobj->query("SELECT IFNULL(@ucount,0) as ucount"));
        if(is_array($feedcountarr) && count($feedcountarr)>0){
            $feedcountarr=$feedcountarr[0];
            $fcount=$feedcountarr["ucount"];
        }
        return $fcount;
    }
   
    
    public function getNewsFeedNotifications($userid,$isCountRequest=true){
        $resultCount=0;
        $lastNotificationIdread=0;
        $oPrivacyType=new PrivacyTypes();
        $notifycountJson="[]";
        $qryToGetLastNotificationIDread="select IFNULL(last_notification_id_read,0) as last_notification_id_read from af_users where user_id=:userid";
        $resultarr= $this->pdodbobj->fetch_array(
                                                $this->pdodbobj->query($qryToGetLastNotificationIDread,
                                                    array(":userid"=>$userid)
                                                ));
        //echo "<pre> query: ".$qryToGetLastNotificationIDread;
//        echo "lastnotificationid: ".$qryToGetLastNotificationIDread;
//        echo "<br>userid: ".$userid;
//        echo "</pre>";
//        die("testing....");                                                
        if(is_array($resultarr) && count($resultarr)>0 ){
            $resultarr=$resultarr[0];
            $lastNotificationIdread=$resultarr["last_notification_id_read"];
        } 
        $objPostNotifyJson[]= new PostNotifyJson();
        $notifyCount=0;
        if($isCountRequest){
            //return array  
            //run a loop and prepare array of action_type and based on action_type call different functions
            //echo "lastnotificationid: ".$lastNotificationIdread." userid: ".$userid;
            $notifyRecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getUniqueFeedIDToNotify($lastNotificationIdread,$userid)"));
            //print_r($notifyRecords);
            if(is_array($notifyRecords) && count($notifyRecords)>0){
                foreach($notifyRecords as $uniqueFeeds){
                    if(isset($uniqueFeeds["Source_Record_ID"]) && is_numeric(trim($uniqueFeeds["Source_Record_ID"]))){
                        $feedid=$uniqueFeeds["Source_Record_ID"];
                        //echo "<br>feedid: ".$feedid;
                        $objPostNotifyJson[$notifyCount]= new PostNotifyJson();
                        $objPostNotifyJson[$notifyCount]->postid=$feedid;
                        $objPostNotifyJson[$notifyCount]->lcount=$this->getFeedLikeCommentShareCount($feedid,"like");
                        $objPostNotifyJson[$notifyCount]->ccount=$this->getFeedLikeCommentShareCount($feedid,"comment");
                        $objPostNotifyJson[$notifyCount]->scount=$this->getFeedLikeCommentShareCount($feedid,"share");
                    }
                    $notifyCount++;
                }
            }
            if(count($objPostNotifyJson)>0){
                //means has some post details
               $notifycountJson= $this->getLikeShareCommentJson($objPostNotifyJson,true);
            }
            //getTotalLikeCountsForFeeds
            return $notifycountJson;
        }
        else{
            //return count
            $notifyRecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getUniqueFeedIDToNotify($lastNotificationIdread,$userid)"));    
            //run a loop and prepare array of action_type and based on action_type call different functions
            //print_r($notifyRecords);
            //die("<br> iUserId:".$userid); 
            return $resultCount;
        }
    }
            
    public function getBirthdayNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_birthday\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
        
    public function getFriendReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_friend_req\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    
    public function getInboxReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_inbox_updates\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    public function getTechnoReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_techno_updates\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    public function getEventsReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_events_updates\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    public function getBlogsReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_blog_updates\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    public function getAppsReqNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_apps_updates\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    
    public function getFollowNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_follwed_req\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }
    
    public function getFriendsAddedNotifications($objDetailJson,$isCountRequest=true){
        $mainJsonStr="[{\"total_friends_added\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr; 
    }    
        
    public function getUserListJson($objDetailJson){
        $mainJsonStr="[{\"total_follwed_req\":{1},\"Data\":{2}}]";
        $detailedJsonStr=$this->getUserDetailsJSON($objDetailJson,false);
        $mainJsonStr=$this->str_format($mainJsonStr,count($objDetailJson), $detailedJsonStr);   
        return $mainJsonStr;
    }
    
    public function getNotifications($userid,$isCountRequest=true){
        $resultCount=0;
        $lastNotificationIdread=0;
        $notifyCount=0;
        $friendsCount=0;
        $oPrivacyType=new PrivacyTypes();
        $qryToGetLastNotificationIDread="select IFNULL(last_notification_id_read,0) as last_notification_id_read from af_users where user_id=:userid";
        $resultarr= $this->pdodbobj->fetch_array(
                                                $this->pdodbobj->query($qryToGetLastNotificationIDread,
                                                    array(":userid"=>$userid)
                                                ));
        if(is_array($resultarr) && count($resultarr)>0 ){
            $resultarr=$resultarr[0];
            $lastNotificationIdread=$resultarr["last_notification_id_read"];
        }
        
        //JSON to show or NOTIFICATION TYPES
        $friendReqJSON[]= new DetailJson();
        $frndJson="[]";
        $friendsCount=0;
        
        $inboxReqJSON[]= new DetailJson();
        $inboxJson="[]";
        $inboxCount=0;
        
        $technoReqJSON[]= new DetailJson();
        $technoJson="[]";
        $technoCount=0;
        
        $eventsReqJSON[]= new DetailJson();
        $eventsJson="[]";
        $eventsCount=0;
        
        $blogsReqJSON[]= new DetailJson();
        $blogsJson="[]";
        $blogsCount=0;
        
        $appsReqJSON[]= new DetailJson();
        $appsJson="[]";
        $appsCount=0;
        
        //echo "last notification id: ".$lastNotificationIdread." userid: ".$userid;
        //run a loop and prepare array of action_type and based on action_type call different functions
        $notifyRecords= $this->pdodbobj->fetch_array($this->pdodbobj->query("CALL getWithoutFeedNotifications($lastNotificationIdread,$userid)"));
        //echo "<pre>";
        //print_r($notifyRecords);
        //echo "</pre>";
        //die("ReadNotifications.php");
        
        if(is_array($notifyRecords) && count($notifyRecords)>0){                
            //print_r($notifyRecords);
            //$this->getFriendReqNotifications()
            foreach($notifyRecords as $newNotification){
                switch(trim($newNotification["Action_Type"])){
                    case "Friend_Request":
                            $UserDetail=$this->getUserDetails($newNotification["User_ID"]);
                            //echo "<pre>";
//                            echo "userid; ".$newNotification["User_ID"];
//					        print_r($UserDetail);
//					        echo "</pre>";
//					        die("ReadNotifications.php");
                            if(is_array($UserDetail) && count($UserDetail)>0){
                                //echo "<br>userid: ".$newNotification["User_ID"];
                                //print_r($UserDetail);
                                //die();
                                $friendReqJSON[$friendsCount]= new DetailJson();
                                $friendReqJSON[$friendsCount]->actionByUserId=$UserDetail["uid"];
                                $friendReqJSON[$friendsCount]->actionByUserName=$UserDetail["uname"];
                                $friendReqJSON[$friendsCount]->actionByUserProfilePicpath=$UserDetail["picpath"];
                                $friendReqJSON[$friendsCount]->actionByUserLink=$UserDetail["plink"];
                                $friendReqJSON[$friendsCount]->actionByUserIsCelibrity=$UserDetail["Is_Celebrity"];
                                $friendReqJSON[$friendsCount]->actionByCity=$UserDetail["Address"];
                                $friendReqJSON[$friendsCount]->actionByCountry=$UserDetail["Country_ID"];
                                
                                $friendsCount++;
                            }
                            break;
                }
                $notifyCount++;
            }
            
        }
        //JSON Format for notifications
        $ReqJSON="[{\"friend_request\":{1},\"inbox_messages\":{2},\"technopath\":{3},\"events\":{4},\"blogs\":{5},\"apps\":{6}}]";
        
        if(count($friendReqJSON)>0 && $friendsCount>0){
            //means has some post details
           $frndJson= $this->getFriendReqNotifications($friendReqJSON,false);
        }
        if(count($inboxReqJSON)>0 && $inboxCount>0){
            //means has some post details
           $inboxJson= $this->getInboxReqNotifications($inboxReqJSON,false);
        }
        if(count($technoReqJSON)>0 && $technoCount>0){
            //means has some post details
           $technoJson= $this->getTechnoReqNotifications($technoReqJSON,false);
        }
        if(count($eventsReqJSON)>0 && $eventsCount>0){
            //means has some post details
           $eventsJson= $this->getEventsReqNotifications($eventsReqJSON,false);
        }
        if(count($blogsReqJSON)>0 && $blogsCount>0){
            //means has some post details
           $blogsJson= $this->getBlogsReqNotifications($blogsReqJSON,false);
        }
        if(count($appsReqJSON)>0 && $appsCount>0){
            //means has some post details
           $appsJson= $this->getAppsReqNotifications($appsReqJSON,false);
        }
        
        $ReqJSON=$this->str_format($ReqJSON,$frndJson,$inboxJson,$technoJson,$eventsJson,$blogsJson,$appsJson);
        //getTotalLikeCountsForFeeds
        return $ReqJSON;
    }
    
    
    public function getMineNotifications($userid,$notificationType="friend_req_found"){
        $resultCount=0;
        $lastNotificationIdread=0;
        $notifyCount=0;
        $friendsCount=0;
        $oPrivacyType=new PrivacyTypes();
        //$notifierDetail=array();
        $limitcount=1;
        switch($notificationType){
            case "birthday_alerts":
                $limitcount=0;
                break;
            default:
                $limitcount=1;
                break;
        }
        
        $limitStr="";
        if($limitcount>0){
            $limitStr=" limit ".$limitcount;
        }
        $qryToGetMineNotifications="SELECT * FROM  af_mine_notifications  where userid=:userid and status=1 and ActivityType='".$notificationType."'".$limitStr;
        $resultarr= $this->pdodbobj->fetch_array(
                                                $this->pdodbobj->query($qryToGetMineNotifications,
                                                    array(":userid"=>$userid)
                                                ));
        
        
        //JSON to show or NOTIFICATION TYPES
        $bdayJSON[]= new DetailJson();
        $bdayJson="[]";
        $bdayCount=0;
        
        $friendReqJSON[]= new DetailJson();
        $frndJson="[]";
        $friendsCount=0;
        
        $friendAddedJSON[]= new DetailJson();
        $frndAddedJson="[]";
        $friendsAddedCount=0;
                        
        $followerJSON[]= new DetailJson();
        $followerJson="[]";
        $followerCount=0;
        
        $followedJSON[]= new DetailJson();
        $followJson="[]";
        $followCount=0;
        
        $inboxReqJSON[]= new DetailJson();
        $inboxJson="[]";
        $inboxCount=0;
        
        $technoReqJSON[]= new DetailJson();
        $technoJson="[]";
        $technoCount=0;
        
        $eventsReqJSON[]= new DetailJson();
        $eventsJson="[]";
        $eventsCount=0;
        
        $blogsReqJSON[]= new DetailJson();
        $blogsJson="[]";
        $blogsCount=0;
        
        $appsReqJSON[]= new DetailJson();
        $appsJson="[]";
        $appsCount=0;
        
        //echo "<pre>";
//        print_r($resultarr);
//        echo "</pre>";
//        die("ReadNotifications.php");
        
        if(is_array($resultarr) && count($resultarr)>0){
            $followCount=0;
            foreach($resultarr as $notifyRow){
                switch(trim($notifyRow["ActivityType"])){
                    case "followed":
                        //echo "matched for userid ".$userid;
                        if(is_numeric($notifyRow["NotifierID"]) && $notifyRow["NotifierID"]>0){
                            //code to fetch record from af_source_follower for Follower_ID = Unique_Field_Value and privacy settings for notifier-user id
                            $notifierDetail=$this->getUserDetails($notifyRow["NotifierID"]);
                            if(is_array($notifierDetail) && count($notifierDetail)>0){
                                //echo "<br>userid: ".$newNotification["User_ID"];
                                //print_r($UserDetail);
                                //die();
                                $followedJSON[$followCount]= new DetailJson();
                                $followedJSON[$followCount]->notificationID=$notifyRow["NotificationID"];
                                $followedJSON[$followCount]->actionByUserId=$notifierDetail["uid"];
                                $followedJSON[$followCount]->actionByUserName=$notifierDetail["uname"];
                                $followedJSON[$followCount]->actionByUserProfilePicpath=$notifierDetail["picpath"];
                                $followedJSON[$followCount]->actionByUserLink=$notifierDetail["plink"];
                                $followedJSON[$followCount]->actionByUserIsCelibrity=$notifierDetail["Is_Celebrity"];
                                $followedJSON[$followCount]->actionByCity=$notifierDetail["Address"];
                                $followedJSON[$followCount]->actionByCountry=$notifierDetail["Country_ID"];
                                                                    
                                $qryToGetFollowedProfiles="SELECT * FROM  ".$notifyRow["Affected_Table"]."  where ".$notifyRow["Unique_Field_Name"]."=:userid";
                                $resultarrFollower= $this->pdodbobj->fetch_array(
                                                                    $this->pdodbobj->query($qryToGetFollowedProfiles,
                                                                        array(":userid"=>$notifyRow["Unique_Field_Value"])
                                                                    ));
                                
                                if(is_array($resultarrFollower) && count($resultarrFollower)>0){
                                        $followerCount=0;
                                    foreach($resultarrFollower as $followerRow){
                                        $followedUserDetail=$this->getUserDetails($followerRow["ID_To_Follow"]);
                                         //print_r($followedUserDetail);
                                        if(is_array($followedUserDetail) && count($followedUserDetail)>0 && is_numeric($followedUserDetail["uid"]) && $followedUserDetail["uid"]>0){
                                            //echo "<br>userid: ".$newNotification["User_ID"];
                                            //print_r($UserDetail);
                                            //die();
                                            $followerJSON[$followerCount]= new DetailJson();
                                            $followerJSON[$followerCount]->notificationID=$followerRow[$notifyRow["Unique_Field_Name"]];
                                            $followerJSON[$followerCount]->actionByUserId=$followedUserDetail["uid"];
                                            $followerJSON[$followerCount]->actionByUserName=$followedUserDetail["uname"];
                                            $followerJSON[$followerCount]->actionByUserProfilePicpath=$followedUserDetail["picpath"];
                                            $followerJSON[$followerCount]->actionByUserLink=$followedUserDetail["plink"];
                                            $followerJSON[$followerCount]->actionByUserIsCelibrity=$followedUserDetail["Is_Celebrity"];
                                            $followerJSON[$followerCount]->actionByCity=$followedUserDetail["Address"];
                                            $followerJSON[$followerCount]->actionByCountry=$followedUserDetail["Country_ID"];
                                            $followerJSON[$followerCount]->info="[]";                                                                                         
                                            $followerCount++;
                                        }
                                    }
                                }
                                if($followerCount>0){                                    
                                    $followedJSON[$followCount]->info=$this->getUserListJson($followerJSON);
                                }
                                else{
                                    $followedJSON[$followCount]->info="[]";
                                }
                                
                                $followCount++;
                            }
                        }
                        
                    break;
                    
                    case "friend_req_found":
                        //echo "matched for userid ".$userid;
                        if(is_numeric($notifyRow["NotifierID"]) && $notifyRow["NotifierID"]>0){
                            $friendsCount=0;
                            //code to fetch record from af_source_follower for Follower_ID = Unique_Field_Value and privacy settings for notifier-user id
                            $notifierDetail=$this->getUserDetails($notifyRow["NotifierID"]);
                            if(is_array($notifierDetail) && count($notifierDetail)>0){
                                //echo "<br>userid: ".$newNotification["User_ID"];
                                //print_r($UserDetail);
                                //die();
                                $friendReqJSON[$friendsCount]= new DetailJson();
                                $friendReqJSON[$friendsCount]->notificationID=$notifyRow["NotificationID"];
                                $friendReqJSON[$friendsCount]->actionByUserId=$notifierDetail["uid"];
                                $friendReqJSON[$friendsCount]->actionByUserName=$notifierDetail["uname"];
                                $friendReqJSON[$friendsCount]->actionByUserProfilePicpath=$notifierDetail["picpath"];
                                $friendReqJSON[$friendsCount]->actionByUserLink=$notifierDetail["plink"];
                                $friendReqJSON[$friendsCount]->actionByUserIsCelibrity=$notifierDetail["Is_Celebrity"];
                                $friendReqJSON[$friendsCount]->actionByCity=$notifierDetail["Address"];
                                $friendReqJSON[$friendsCount]->actionByCountry=$notifierDetail["Country_ID"];
                                $friendReqJSON[$friendsCount]->info="[]";
                                
                                $friendsCount++;
                            }
                        }
                        
                    break;                    
                    
                    case "friend_added":
                        //echo "matched for userid ".$userid;
                        if(is_numeric($notifyRow["NotifierID"]) && $notifyRow["NotifierID"]>0){
                            $friendsAddedCount=0;
                            //code to fetch record from af_source_follower for Follower_ID = Unique_Field_Value and privacy settings for notifier-user id
                            $notifierDetail=$this->getUserDetails($notifyRow["NotifierID"]);
                            if(is_array($notifierDetail) && count($notifierDetail)>0){
                                //echo "<br>userid: ".$newNotification["User_ID"];
                                //print_r($UserDetail);
                                //die();
                                $friendAddedJSON[$friendsAddedCount]= new DetailJson();
                                $friendAddedJSON[$friendsAddedCount]->notificationID=$notifyRow["NotificationID"];
                                $friendAddedJSON[$friendsAddedCount]->actionByUserId=$notifierDetail["uid"];
                                $friendAddedJSON[$friendsAddedCount]->actionByUserName=$notifierDetail["uname"];
                                $friendAddedJSON[$friendsAddedCount]->actionByUserProfilePicpath=$notifierDetail["picpath"];
                                $friendAddedJSON[$friendsAddedCount]->actionByUserLink=$notifierDetail["plink"];
                                $friendAddedJSON[$friendsAddedCount]->actionByUserIsCelibrity=$notifierDetail["Is_Celebrity"];
                                $friendAddedJSON[$friendsAddedCount]->actionByCity=$notifierDetail["Address"];
                                $friendAddedJSON[$friendsAddedCount]->actionByCountry=$notifierDetail["Country_ID"];
                                                                    
                                $qryToGetFollowedProfiles="SELECT * FROM  ".$notifyRow["Affected_Table"]."  where ".$notifyRow["Unique_Field_Name"]."=:uniqueid";
                                $resultarrFollower= $this->pdodbobj->fetch_array(
                                                                    $this->pdodbobj->query($qryToGetFollowedProfiles,
                                                                        array(":uniqueid"=>$notifyRow["Unique_Field_Value"])
                                                                    ));
                                
                                if(is_array($resultarrFollower) && count($resultarrFollower)>0){
                                    $followerCount=0;
                                    foreach($resultarrFollower as $followerRow){
                                        $followedUserDetail=$this->getUserDetails($followerRow["User_Friend_ID"]);
                                         //print_r($followedUserDetail);
                                        if(is_array($followedUserDetail) && count($followedUserDetail)>0 && is_numeric($followedUserDetail["uid"]) && $followedUserDetail["uid"]>0){
                                            //echo "<br>userid: ".$newNotification["User_ID"];
                                            //print_r($UserDetail);
                                            //die();
                                            $followerJSON[$followerCount]= new DetailJson();
                                            $followerJSON[$followerCount]->notificationID=$followerRow[$notifyRow["Unique_Field_Name"]];
                                            $followerJSON[$followerCount]->actionByUserId=$followedUserDetail["uid"];
                                            $followerJSON[$followerCount]->actionByUserName=$followedUserDetail["uname"];
                                            $followerJSON[$followerCount]->actionByUserProfilePicpath=$followedUserDetail["picpath"];
                                            $followerJSON[$followerCount]->actionByUserLink=$followedUserDetail["plink"];
                                            $followerJSON[$followerCount]->actionByUserIsCelibrity=$followedUserDetail["Is_Celebrity"];
                                            $followerJSON[$followerCount]->actionByCity=$followedUserDetail["Address"];
                                            $followerJSON[$followerCount]->actionByCountry=$followedUserDetail["Country_ID"];
                                            $followerJSON[$followerCount]->info="[]";                                                                                         
                                            $followerCount++;
                                        }
                                    }
                                }
                                if($followerCount>0){                                    
                                    $friendAddedJSON[$friendsAddedCount]->info=$this->getUserListJson($followerJSON);
                                }
                                else{
                                    $friendAddedJSON[$friendsAddedCount]->info="[]";
                                }
                                
                                $friendsAddedCount++;
                            }
                        }
                        
                    break;
                    
                    case "birthday_alerts":
                        //echo "matched for userid ".$notifyRow["NotifierID"];
                        //die();
                        if(is_numeric($notifyRow["NotifierID"]) && $notifyRow["NotifierID"]>0){
                            $bdayCount=0;
                            //code to fetch record from af_source_follower for Follower_ID = Unique_Field_Value and privacy settings for notifier-user id
                            $notifierDetail=$this->getUserDetails($notifyRow["NotifierID"]);
                            if(is_array($notifierDetail) && count($notifierDetail)>0){
                                //echo "<br>userid: ".$newNotification["User_ID"];
                                //print_r($UserDetail);
                                //die();
                                $bdayJSON[$bdayCount]= new DetailJson();
                                $bdayJSON[$bdayCount]->notificationID=$notifyRow["NotificationID"];
                                $bdayJSON[$bdayCount]->daysLeft=$notifyRow["nevent_date"];
                                $bdayJSON[$bdayCount]->actionByUserId=$notifierDetail["uid"];
                                $bdayJSON[$bdayCount]->actionByUserName=$notifierDetail["uname"];
                                $bdayJSON[$bdayCount]->actionByUserProfilePicpath=$notifierDetail["picpath"];
                                $bdayJSON[$bdayCount]->actionByUserLink=$notifierDetail["plink"];
                                $bdayJSON[$bdayCount]->actionByUserIsCelibrity=$notifierDetail["Is_Celebrity"];
                                $bdayJSON[$bdayCount]->actionByCity=$notifierDetail["Address"];
                                $bdayJSON[$bdayCount]->actionByCountry=$notifierDetail["Country_ID"];
                                $bdayJSON[$bdayCount]->info="[]";
                                
                                $bdayCount++;
                            }
                        }
                        break;
                }
                //die("ReadNotifications.php");
            }
        }
        
        //JSON Format for notifications
        $ReqJSON="[{\"friend_request\":{1},\"inbox_messages\":{2},\"technopath\":{3},\"events\":{4},\"blogs\":{5},\"apps\":{6},\"follows\":{7},\"friends_added\":{8},\"birthday_alerts\":{9}}]";
        
        if(count($friendReqJSON)>0 && $friendsCount>0){
            //means has some post details
           $frndJson= $this->getFriendReqNotifications($friendReqJSON,false);
        }
        if(count($inboxReqJSON)>0 && $inboxCount>0){
            //means has some post details
           $inboxJson= $this->getInboxReqNotifications($inboxReqJSON,false);
        }
        if(count($technoReqJSON)>0 && $technoCount>0){
            //means has some post details
           $technoJson= $this->getTechnoReqNotifications($technoReqJSON,false);
        }
        if(count($eventsReqJSON)>0 && $eventsCount>0){
            //means has some post details
           $eventsJson= $this->getEventsReqNotifications($eventsReqJSON,false);
        }
        if(count($blogsReqJSON)>0 && $blogsCount>0){
            //means has some post details
           $blogsJson= $this->getBlogsReqNotifications($blogsReqJSON,false);
        }
        if(count($appsReqJSON)>0 && $appsCount>0){
            //means has some post details
           $appsJson= $this->getAppsReqNotifications($appsReqJSON,false);
        }
        if(count($followJson)>0 && $followCount>0){
            //means has some post details
           $followJson= $this->getFollowNotifications($followedJSON,false);
        }
        if(count($friendAddedJSON)>0 && $friendsAddedCount>0){
            //means has some post details
           $frndAddedJson= $this->getFriendsAddedNotifications($friendAddedJSON,false);
        }
       
       if(count($bdayJSON)>0 && $bdayCount>0){
            //means has some post details
           $bdayJson= $this->getBirthdayNotifications($bdayJSON,false);
        }
        
        $ReqJSON=$this->str_format($ReqJSON,$frndJson,$inboxJson,$technoJson,$eventsJson,$blogsJson,$appsJson,$followJson,$frndAddedJson,$bdayJson);
        //getTotalLikeCountsForFeeds
        return $ReqJSON;
    }
}


?>